// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/login_register/login.vue'
import Register from '../components/login_register/register.vue'
import PersonalInfo from '../views/UserCenter.vue'
import LearningHistory from '../views/LearningHistory.vue' // 新增导入

import CourseSelectionView from '../views/CourseSelectionView.vue'

import CourseInfo from '@/views/Course/CourseInfo.vue'
import CourseHomework from '@/views/Course/CourseHomework.vue'
import TeachingMaterial from '@/views/Course/TeachingMaterial.vue'
import CourseLayout from '@/views/Course/CourseLayout.vue'
import MyClass from '@/views/Course/MyClass.vue'
import TeachingMaterialType from '@/views/Course/TeachingMaterialType.vue'
import TeachingMaterialLabel from '@/views/Course/TeachingMaterialLabel.vue'
import UploadTeachingMaterial from '@/views/Course/UploadTeachingMaterial.vue'
import TeachingManagement from '@/views/TeachingManagement.vue'
import ExerciseCenter from '@/views/ExerciseCenter.vue'

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
  },
  {
    path: '/mainPage',
    name: 'CourseSelection',
    component: CourseSelectionView,
  },
  {
    path: '/create-course',
    name: 'CreateCourse',
    component: () => import('@/views/Course/CreateCourse.vue'),
  },
  {
    path: '/join-course',
    name: 'JoinCourse',
    component: () => import('@/views/Course/JoinCourseView.vue'),
  },
  {
    path: '/course/:id', // 父路由的 :id 对应 CourseLayout 的 props
    component: CourseLayout,
    children: [
      { path: 'info', component: CourseInfo },
      {
        path: 'exercise',
        component: ExerciseCenter,
      },
      {
        path: 'material',
        component: UploadTeachingMaterial,
        children: [
          { path: 'by-chapter', component: TeachingMaterial },
          { path: 'by-type', component: TeachingMaterialType },
          { path: 'by-label', component: TeachingMaterialLabel },
        ],
      },
      {
        path: 'manage',
        component: TeachingManagement, // 教学管理父级容器
        children: [
          {
            path: 'course-maintenance',
            name: 'CourseMaintenanceEdit',
            component: () => import('@/components/Teaching_Management/CourseMaintenanceForm.vue'),
            props: (route) => ({
              courseId: route.params.id,
            }),
          },
          {
            path: 'students/:classId?', // 班级学生路径，classId 变为可选参数
            name: 'ClassStudentsManagement',
            component: () => import('@/components/Teaching_Management/ClassStudentsManagement.vue'),
            props: (route) => ({
              courseId: route.params.id, // 将父路由的课程ID作为 courseId prop
              classId: route.params.classId, // 将 classId 作为 classId prop 传递
            }),
          },
          {
            path: 'team/:classId?', // 教学团队路径，classId 是可选的
            name: 'TeachingTeamManagement',
            component: () => import('@/components/Teaching_Management/TeachingTeamManagement.vue'),
            props: (route) => ({
              courseId: route.params.id, // 将父路由的课程ID作为 courseId prop
              classId: route.params.classId, // 将 classId 作为 classId prop 传递
            }),
          },
        ],
      },
      { path: 'class', component: MyClass },
    ],
  },
  {
    path: '/userCenter',
    component: PersonalInfo,
  },
  {
    path: '/learningHistory/:courseId', // 新增路由配置
    name: 'LearningHistory', // 路由名称，与跳转函数中一致
    component: LearningHistory, // 对应的组件
  },
  {
    path: '/exercise',
    component: ExerciseCenter,
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 从localStorage获取token
  const token = localStorage.getItem('token')

  // 不需要登录就能访问的路由
  const publicPages = ['/login', '/register']
  const authRequired = !publicPages.includes(to.path)

  // 如果需要认证但没有token，重定向到登录页面
  if (authRequired && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
